﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dot5Demo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            var textRender = new TextRenderer()
            {
                Text = "测试文字"
            };
            var ctr = new BaseCtr()
            {
                BackColor = Color.LightSalmon,
                Text = "背景控件",
                Size = new Size(200, 300),
                Location = new Point(90, 90)
            };
            ctr.AddRenderer(textRender);
            this.Controls.Add(ctr);
            this.notifyIcon1.Visible = true;

            this.ClientSizeChanged += MainForm_ClientSizeChanged;
            this.contextMenuStrip1.ItemClicked += ContextMenuStrip1_ItemClicked;
            this.FormClosing += MainForm_FormClosing;
            this.menuStrip1.ItemClicked += MenuStrip1_ItemClicked;
            InitMenu();
            this.UseCulture();
            this.notifyIcon1.UseCulture();
            ResourceExtensions.CultureChanged += ResourceExtensions_CultureChanged;

            this.Text = this.Text + " --- " + Environment.Version.ToString();
        }

        private void ResourceExtensions_CultureChanged(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<object, EventArgs>((s, e) =>
                 {
                     ResourceExtensions_CultureChanged(sender, e);
                 }));
                return;
            }
            this.UseCulture();
        }

        private Dictionary<string, List<string>> _mainItems = new Dictionary<string, List<string>>()
        {
            {"File",new List<string>(){"New","Open","Save","SaveAs","Exit" } },
            {"Edit",null },
            {"View",null },
            {"Project",null },
            {"Build",null },
            {"Debug",null },
            {"Test",new List<string>(){"富文本显示测试" }  },
            {"Tools",new List<string>(){"Settings" } },
            {"Extensions",null},
            {"Help",new List<string>(){"About" } },
        };

        private void InitMenu()
        {
            var mainMenu = this.menuStrip1;
            foreach (var item in _mainItems)
            {
                var tsl = new ToolStripMenuItem()
                {
                    Text = item.Key.CultureString()
                };
                if (item.Value != null)
                {
                    item.Value.ForEach(i =>
                    {
                        tsl.DropDownItems.Add(new ToolStripMenuItem()
                        {
                            Text = i.CultureString(),
                            Tag = i,
                        });
                    });
                    tsl.DropDownItemClicked += Tsl_DropDownItemClicked;
                }
                mainMenu.Items.Add(tsl);
            }
        }

        private Form _settings;
        private void Tsl_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            var item = e.ClickedItem;
            if (item.Tag == null)
                return;
            switch (item.Tag.ToString().ToLower())
            {
                case "settings":
                    if (_settings != null
                        && !_settings.IsDisposed
                        && !_settings.Disposing)
                    {
                        if (!_settings.Visible)
                        {
                            _settings.Show(this);
                        }
                        _settings.BringToFront();
                    }
                    else
                    {
                        _settings = new Frms.FrmSettings();
                        _settings.Show(this);
                        return;
                    }
                    break;
                case "exit":
                    _askExit = true;
                    this.Close();
                    break;
                case "富文本显示测试":
                    var fm = new FrmRichTextBox();
                    fm.ShowDialog();
                    break;
                default:
                    break;
            }
        }

        private bool _askExit = false;

        private void MenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_askExit)
                return;
            if (this.tlsmi_hide.Checked)
            {
                if (e.CloseReason != CloseReason.ApplicationExitCall)
                {
                    e.Cancel = true;
                    HideToNotify();
                }
            }
        }

        private void ContextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            var item = e.ClickedItem as ToolStripMenuItem;
            switch (item.Name)
            {
                case "tlsmi_exit":
                    Application.Exit();
                    break;
                case "tlsmi_showMain":
                    notifyIcon1_MouseDoubleClick(null, null);
                    break;
                case "tlsmi_other":
                    MessageBox.Show("事件点击来自 托盘图标的菜单", "其他功能");
                    break;
                case "tlsmi_banMsg":
                    if (!item.Checked)
                    {
                        MessageBox.Show("事件点击来自 托盘图标的菜单", "禁用通知");
                        item.Text = "启用通知";
                    }
                    else
                    {
                        MessageBox.Show("事件点击来自 托盘图标的菜单", "启用通知");
                        item.Text = "禁用通知";
                    }
                    item.Checked = !item.Checked;
                    break;
                case "tlsmi_hide":
                    item.Checked = !item.Checked;
                    break;
                default:
                    break;
            }
        }

        private void MainForm_ClientSizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
                this.ShowInTaskbar = false;
                HideToNotify();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                ShowFromNotify();
            }
        }

        private void HideToNotify()
        {
            this.Visible = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
        }

        private void ShowFromNotify()
        {
            this.Visible = true;
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }
    }
}
